package com.totoro.controller;

import com.totoro.pojo.DayCount;
import com.totoro.pojo.Seller;
import com.totoro.pojo.Shop;
import com.totoro.pojo.User;
import com.totoro.service.SellerService;
import com.totoro.service.ShopService;
import com.totoro.utils.ReturnFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author TOTORO
 * @date 2021/9/1 0:53
 */
@Controller
public class SellerIndexController {

    @Autowired
    private ShopService shopService;
    @Autowired
    private SellerService sellerService;

    @ResponseBody
    @RequestMapping("/getShopDayAccessCount")
    public String getShopDayAccessCount(HttpSession session){
        User user = (User) session.getAttribute("user");
        if(user == null){
            return ReturnFormat.retData("error", false);
        }
        // 判断用户是否为卖家
        Seller seller = sellerService.findSellerIdByUserId(user.getUserId());
        if(null == seller){
            return ReturnFormat.retData("error", false);
        }
        // 获得店铺日访问量
        Shop shop = shopService.findShopBySlid(seller.getId());
        List<DayCount> dayCounts = shopService.findShopDayAccessCount(shop.getId());
        // 获得长度，没有的小时用0代替
        int dayCountLength = dayCounts.size();
        for (int i = dayCountLength; i < 24; i++) {
            dayCounts.add(new DayCount(i+1, 0));
        }
        return ReturnFormat.retData(dayCounts, true);
    }

    @ResponseBody
    @RequestMapping("/getShopDayPays")
    public String getShopDayPays(HttpSession session){
        User user = (User) session.getAttribute("user");
        if(user == null){
            return ReturnFormat.retData("error", false);
        }
        // 判断用户是否为卖家
        Seller seller = sellerService.findSellerIdByUserId(user.getUserId());
        if(null == seller){
            return ReturnFormat.retData("error", false);
        }
        // 获得店铺日访问量
        Shop shop = shopService.findShopBySlid(seller.getId());
        List<DayCount> dayCounts = shopService.findShopDayPayMoney(shop.getId());
        // 获得长度，没有的小时用0代替
        int dayCountLength = dayCounts.size();
        for (int i = dayCountLength; i < 24; i++) {
            dayCounts.add(new DayCount(i+1, 0));
        }
        return ReturnFormat.retData(dayCounts, true);
    }

    @ResponseBody
    @RequestMapping("/getShopDayPayChildOrderCount")
    public String getShopDayPayChildOrderCount(HttpSession session){
        User user = (User) session.getAttribute("user");
        if(user == null){
            return ReturnFormat.retData("error", false);
        }
        // 判断用户是否为卖家
        Seller seller = sellerService.findSellerIdByUserId(user.getUserId());
        if(null == seller){
            return ReturnFormat.retData("error", false);
        }
        // 获得店铺日访问量
        Shop shop = shopService.findShopBySlid(seller.getId());
        List<DayCount> dayCounts = shopService.findShopDayPayChildOrder(shop.getId());
        // 获得长度，没有的小时用0代替
        int dayCountLength = dayCounts.size();
        for (int i = dayCountLength; i < 24; i++) {
            dayCounts.add(new DayCount(i+1, 0));
        }
        return ReturnFormat.retData(dayCounts, true);
    }

    @ResponseBody
    @RequestMapping("/findShopDayPayUserCount")
    public String findShopDayPayUserCount(HttpSession session){
        User user = (User) session.getAttribute("user");
        if(user == null){
            return ReturnFormat.retData("error", false);
        }
        // 判断用户是否为卖家
        Seller seller = sellerService.findSellerIdByUserId(user.getUserId());
        if(null == seller){
            return ReturnFormat.retData("error", false);
        }
        // 获得店铺日访问量
        Shop shop = shopService.findShopBySlid(seller.getId());
        List<DayCount> dayCounts = shopService.findShopDayPayUserCount(shop.getId());
        // 获得长度，没有的小时用0代替
        int dayCountLength = dayCounts.size();
        for (int i = dayCountLength; i < 24; i++) {
            dayCounts.add(new DayCount(i+1, 0));
        }
        return ReturnFormat.retData(dayCounts, true);
    }
}
